package cn.fastoo.common.dao.system;

import cn.fastoo.common.entity.system.Permission;
import cn.fastoo.common.entity.system.Role;
import cn.fastoo.common.sampleframe.dao.FastDao;
import cn.fastoo.common.sampleframe.dao.impl.FastDaoImpl;
import cn.fastoo.common.sampleframe.search.Search;
import cn.fastoo.common.utils.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Component
public class PermissionDao extends FastDaoImpl<Permission> implements FastDao<Permission> {


    /**
     * 根据ID获取实体
     *
     * @param id
     * @return
     */
    public Permission getPermission(Long id) {
        return findOne(new Search().addSearchFilter("EQ_id", id));
    }


    /**
     * 获取资源权限列表
     *
     * @return
     */
    public List<Permission> getResourcePermission() {
        return find(new Search().addSearchFilter("EQ_type", 1));
    }

    /**
     * 获取角色包含权限
     *
     * @param roleId
     * @return
     */
    public List<Permission> getRolePermissions(Role role) {
        if(StringUtils.isBlank(role.getPermissionIds())){
            return null;
        }
        return find(new Search().addSearchFilter("IN_id", role.getPermissionIds()));
    }

    /**
     * 获取角色未包含但是可以选择的权限
     *
     * @param roleId
     * @return
     */
    public List<Permission> findRoleChoosablePermission(Role role, PageRequest pageRequest) {
        List<Permission> permissionList = this.getRolePermissions(role);
        List<Long> idList = new ArrayList<Long>();
        idList.add(1L);// 超级权限不予分配
        for (Permission permission : permissionList) {
            idList.add(permission.getId());
        }
        return find(new Search().addSearchFilter("NOTIN_id", idList));
    }

    /**
     * @param pageRequest
     * @return
     */
    public List<Permission> getRoleChoosePermissionPage(PageRequest pageRequest) {
        return find(new Search().addSearchFilter("NEQ_id", Permission.SUPER_ID));
    }


 
}
